import { getAreaObj } from '@/utils/util'

export const formConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'top',
	labelWidth: '120px',
	column: [
		{
			label: '机构名称',
			prop: 'organizationName',
			propName: 'organizationName',
			type: 'remoteSelect',
			api: 'view_dpServiceOrgBase_page',
			filterKey: 'organizationName',
			filterLabel: 'organizationName',
			multiple: false,
			rules: [{ required: true, message: '请选择机构名称' }],
			span: 24,
			change: (item, form, value) => {
				form.organizationType = 1
			},
		},
		{
			label: '规则状态',
			prop: 'ruleStatus',
			type: 'radio',
			dicData: [
				{
					label: '启用',
					value: 1,
				},
				{
					label: '停用',
					value: 0,	
				},
			],
			rules: [{ required: true, message: '请选择规则状态' }],
			span: 24,
		},
		{
			type: 'DividerHtml',
			modelValue: '白名单老人设置',
			labelPosition: 'top',
			span: 24,
		},
		{
			label: '阈值类型',
			prop: 'oldmanSetType',
			type: 'radio',
			dicData: [
				{
					label: '人数',
					value: 0,
				},
				{
					label: '不限',
					value: -1,
				},
			],
			control: (e, model) => {
				return {
					oldmanLimit: {
						display: e == 0,
					},
				}
			},
			change: (item, form, value) => {
				if(form.oldmanSetType == -1 && form.oldmanLimit) {
					form.oldmanLimit = ''
				}
			},
			rules: [{ required: true, message: '请选择阈值类型' }],
			span: 24,
		},
		{
			label: '阈值',
			type: 'input',
			prop: 'oldmanLimit',
			rules: [{ required: true, pattern: /^[0-9]*$/, message: '请输入正确的阈值' }],
			span: 24,
		},
		{
			type: 'DividerHtml',
			modelValue: '白名单员工设置',
			labelPosition: 'top',
			span: 24,
		},
		{
			label: '阈值类型',
			prop: 'staffSetType',
			type: 'radio',
			dicData: [
				{
					label: '人数',
					value: 0,
				},
				{
					label: '不限',
					value: -1,
				},
			],
			control: (e, model) => {
				return {
					staffLimit: {
						display: e == 0,
					},
				}
			},
			change: (item, form, value) => {
				if(form.staffSetType == -1 && form.staffLimit) {
					form.staffLimit = ''
				}
			},
			rules: [{ required: true, message: '请选择阈值类型' }],
			span: 24,
		},
		{
			label: '阈值',
			prop: 'staffLimit',
			rules: [{ required: true, pattern: /^[0-9]*$/, message: '请输入正确的阈值' }],
			span: 24,
		},
	],
}

export const searchConfig = {
	inline: true,
	showBtn: true,
	labelPosition: 'left',
	column: [
		{
			label: '所属区',
			prop: 'areas',
			type: 'AplidAreaSelect',
			regionGrade: 3,
			span: 8,
		},
		{
			label: '机构名称',
			prop: 'organizationName',
			type: 'input',
			span: 8,
		},
		{
			label: '状态',
			prop: 'ruleStatus',
			type: 'select',
			dicData: [
				{ label: '停用', value: 0 },
				{ label: '启用', value: 1 },
			],
			span: 8,
		},
		{
			label: '创建人',
			prop: 'createUserName',
			type: 'input',
			span: 8,
		},
		{
			label: '创建时间',
			prop: 'createTimeBetween',
			type: 'dateBetween',
			format:'YYYY年MM月DD HH:mm:ss',
			valueFormat:"YYYY-MM-DD HH:mm:ss",
			span: 8,
		},
	],
}

export const tableConfig = {
	stripe: true,
	border: true,
	columns: [
		{ label: '所属区', prop: 'areaName' },
		{ label: '机构名称', prop: 'organizationName' },
		{ label: '白名单老人阈值', prop: 'oldmanLimit' },
		{ label: '白名单员工阈值', prop: 'staffLimit' },
		{ 
			label: '状态',
			prop: 'ruleStatus',
			custome: 'tag',
			dicData: [
				{
					label: '停用',
					value: 0,
					type: 'danger'
				},
				{
					label: '启用',
					value: 1,
					type: 'success'
				},
			],
		},
		{ label: '创建人', prop: 'createUserName' },
		{ label: '创建时间', prop: 'createTime' },
		{
			label: '操作',
			prop: 'menu',
			width: 200,
			fixed: 'right',
		},
	],
}

export const api = {
	index: 'face_whiteListRule_page',
	add: 'face_whiteListRule_add',
	edit: 'face_whiteListRule_edit',
	delete: 'face_whiteListRule_del',
	authArea: 'authArea',
}
export const baseConfig = {
	pageName: '拍照巡查白名单设置',
	dictcodes: ['TYPE'],
	rowKey: 'id',
	addLabel: '新增',
	tipLabel: 'organizationName',
	exportMqTopic: '/ning/export/',
	api,
	paramsObj: {
		organizationTypeList: [3],
	},
	auth: {
		...api,
		mulitSet: 'mulitSet',
	},
	menuBtn: [
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: api.edit,
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: api.delete,
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '55%',
	},
	searchConfig,
	formConfig,
	tableConfig,
	detailConfig: formConfig,
	saveFormate: async (form) => {
		return new Promise((resolve, reject) => {
			if(!form.staffLimit) {
				form.staffLimit = -1
			}
			if(!form.oldmanLimit) {
				form.oldmanLimit = -1
			}
			form.areaId = JSON.parse(sessionStorage.getItem('apld-xningxiang-userInfo')).content.authArea
			form.areaName = JSON.parse(sessionStorage.getItem('apld-xningxiang-userInfo')).content.organizationName
			form.oldmanLimit = Number(form.oldmanLimit)
			form.staffLimit = Number(form.staffLimit)
			resolve(form)
		})
	},
	beforeOnLoadFormate: async (params) => {
		return new Promise((resolve, reject) => {
			const areaObj: any = getAreaObj(params.areas)
			resolve({
				...params,
				authProvince: areaObj.provinceId,
				authCity: areaObj.cityId,
				authArea: areaObj.areaId,
				areas: undefined,
			})
		})
	},
	onLoadFormate: (arr: any) => {
		return new Promise((resolve, reject) => {
			resolve(
				arr.map((item) => {
					if(item.oldmanLimit == -1) {
						item.oldmanLimit = ''
					}
					if(item.staffLimit == -1) {
						item.staffLimit = ''
					}
					if(item.oldmanLimit > 0) {
						item.oldmanSetType = 0
					} else {
						item.oldmanSetType = -1
					}
					if(item.staffLimit > 0) {
						item.staffSetType = 0
					} else {
						item.staffSetType = -1
					}
					return item
				})
			)
		})
	},
}
